package app.qrcode.browser.hearttoheart.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.adapter.FragmentStateAdapter
import app.qrcode.browser.hearttoheart.databinding.FragmentHomeBinding
import app.qrcode.browser.hearttoheart.fragments.*
import com.google.android.material.tabs.TabLayoutMediator

class HomeFragment : Fragment() {


    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root


        binding.funtionVP.adapter = object : FragmentStateAdapter(this) {


            override fun getItemCount(): Int = 3


            override fun createFragment(position: Int): Fragment =
                when (position) {
                    0 -> StudyFragment()
                    1 -> LessonPrepareFragment()
                    else -> ClassControlFragment()
                }
        }
        TabLayoutMediator(binding.tabLayout, binding.funtionVP) { tab, position ->
            when (position) {
                0 -> tab.text = "学习"
                1 -> tab.text = "备课"
                else -> tab.text = "班级管理"
            }
        }.attach()
        return root
    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}